﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Sobiens.Web.Controls.ItemSelector.Samples.BLL;
using System.Web.UI.HtmlControls;

namespace Sobiens.Web.Controls.ItemSelector.Samples.Dialogs.Selectors
{
    public partial class ItemSelector : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "Parca/Cihaz Secimi";
            ItemsGridView.RowDataBound += new GridViewRowEventHandler(ItemsGridView_RowDataBound);
            if (Page.IsPostBack == false)
            {
            }
        }

        protected void SearchImageButton_Click(object sender, ImageClickEventArgs e)
        {
            SearchByKeyword();
        }

        protected void KeywordTextBox_TextChanged(object sender, EventArgs e)
        {
            SearchByKeyword();
        }

        private void SearchByKeyword()
        {
            DataSet ds = ItemsManager.SearchItems(KeywordTextBox.Text);
            ItemsGridView.DataSource = ds;
            ItemsGridView.DataBind();
        }


        void ItemsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
                return;
            DataRow row = ((DataRowView)e.Row.DataItem).Row;
            string itemID = row["Item_ID"].ToString();
            string itemName = row["ItemName"].ToString();
                
            HtmlGenericControl hyperlink = new HtmlGenericControl();
            hyperlink.InnerHtml = string.Format("<a href='javascript:void(0)' onclick=\"window.parent.$find('{0}').addItem('{0}', '{1}', '{2}')\"><img border='0' src='../../images/NEWITEM.GIF' /></a>", Request["MainControlID"], itemName, itemID);
            e.Row.Cells[0].Controls.Add(hyperlink);

            //string id = row["Item_ID"].ToString();
            //string name = ItemsManager.GetItemFullName(row);
            //name = name.Replace("\"", " ").Replace("'", " ");
            ////string html = "<div onMouseOver=\"style.cursor='hand'\" onclick=\"alert(window.dialogArguments);alert(window.dialogArguments.OpenerWindow.AddItem);window.dialogArguments.OpenerWindow.AddItem(" + id + ",'" + name + "');window.dialogArguments.OpenerWindow.GenerateItemTable();\"><img src=\"images/newuser.gif\"></div>";
            //string html = "<a href=\"javascript:void(0)\" onclick=\"AddItem(" + id + ",'" + name + "');\"><img src=\"/images/newuser.gif\"></a>";
            //e.Row.Cells[0].Text = html;
            //e.Row.Cells[1].Text = name;
        }
    }
}